package com.source.hotelbath.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ffcs.network.connect.CustomerError;
import com.ffcs.network.connect.ResponseListener;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.source.hotelbath.MainActivity;
import com.source.hotelbath.R;
import com.source.hotelbath.base.BizActivity;
import com.source.hotelbath.net.CustomerRequestConstants;
import com.source.hotelbath.net.LoadingDialog;
import com.source.hotelbath.net.request.LoginRequest;
import com.source.hotelbath.net.response.LoginBean;
import com.source.hotelbath.utils.DialogCallback;
import com.source.hotelbath.utils.MyCookieStore;
import com.source.hotelbath.utils.SharedPreferencesUtils;

import java.io.IOException;

public class LoginActivity extends BizActivity{


    private Button btn_login;
    private EditText et_user_name;
    private ImageView iv_clear_user_name;
    private EditText et_pwd;
    private ImageView iv_clear_pwd;
    private String userName;
    private String password;
    private TextView tv_forget_pwd;


    @Override
    protected void onBaseCreate(Bundle var1) {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void bindView(Bundle var1) {
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        iv_clear_user_name = (ImageView) findViewById(R.id.iv_clear_user_name);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        iv_clear_pwd = (ImageView) findViewById(R.id.iv_clear_pwd);
        btn_login = (Button) findViewById(R.id.btn_login);
        tv_forget_pwd = (TextView) findViewById(R.id.tv_forget_pwd);
        tv_forget_pwd.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        iv_clear_pwd.setOnClickListener(this);
        iv_clear_user_name.setOnClickListener(this);
        MyCookieStore.removeCookie(this);
        et_user_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (et_user_name.getText().toString().length() > 0) {
                        iv_clear_user_name.setVisibility(View.VISIBLE);
                    } else {
                        iv_clear_user_name.setVisibility(View.GONE);
                    }
                } else {
                    iv_clear_user_name.setVisibility(View.GONE);
                }
            }
        });
        et_pwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (et_pwd.getText().toString().length() > 0) {
                        iv_clear_pwd.setVisibility(View.VISIBLE);
                    } else {
                        iv_clear_pwd.setVisibility(View.GONE);
                    }
                } else {
                    iv_clear_pwd.setVisibility(View.GONE);
                }
            }
        });
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view==btn_login){
            if (chekcLoginInfo()){
                String loginUrl=CustomerRequestConstants.newUrl+CustomerRequestConstants.LOGIN_COMMOND+"?phone="+userName+"&password="+password;
                SharedPreferencesUtils.saveStringData(this,"loginUrl",loginUrl);
                try {
                    OkGo.<LoginBean>get(loginUrl)//
                            .tag(this)//
                            .execute(new DialogCallback<LoginBean>(this,true) {
                                @Override
                                public void onSuccess(Response<LoginBean> response) {
                                    LoginBean loginBean=response.body();
                                    if (loginBean.isSuccess()==true){
                                        SharedPreferencesUtils.saveStringData(LoginActivity.this,"phone",loginBean.getData().getPhone());
                                        SharedPreferencesUtils.saveStringData(LoginActivity.this,"name",loginBean.getData().getName());
                                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }else {
                                        Toast.makeText(LoginActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
                                    }

                                }

                                @Override
                                public void onError(Response<LoginBean> response) {
                                    super.onError(response);
                                    Toast.makeText(LoginActivity.this,"请求网络失败",Toast.LENGTH_SHORT).show();
                                }

                            });
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }else if(view==iv_clear_pwd){
          et_pwd.setText("");
        }else if(view==iv_clear_user_name){
          et_user_name.setText("");
        }else if(view==tv_forget_pwd){
            //忘记密码跳转页面
            Intent intent =new Intent(this,ForgetActivity.class);
            startActivity(intent);
        }
    }


    private boolean chekcLoginInfo() {
        boolean result = true;
        userName = et_user_name.getText().toString();
        password = et_pwd.getText().toString();
           //少了手机号的正则判断
        if (TextUtils.isEmpty(userName)) {
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            result = false;
        }  else if (TextUtils.isEmpty(password)) {
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            result = false;
        }
        return result;
    }

}
